package cn.bopomo.dealer.base.action.ajax;

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONObject;

import cn.bopomo.dealer.base.action.BaseAction;
import cn.bopomo.dealer.base.common.ServiceConfig;
import cn.bopomo.dealer.base.service.IOperatorService;
import cn.bopomo.dealer.base.vo.AuGroup;
import cn.bopomo.dealer.base.vo.OperatorDetail;

public class LoadOPGroupAjax extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-control","no-cache");
		
		IOperatorService os = (IOperatorService)this.findService(ServiceConfig.OPERATOR_SERVICE);
		String oUid = request.getParameter("oid");
		String nUid = this.getUserId(request);
		
		OperatorDetail oOP = null;
		List oGroupList = null;
		JSONObject json = new JSONObject();
		json.put("pid",nUid);
		if (oUid != null && !"".equals(oUid)){
			oOP = os.getOperatorDetail(oUid);
			oGroupList = os.getGroupListByUser(oUid);
			if (oOP != null)
				json.put("moduleAuthorize",oOP.getAuthorize());
			json.put("id",oOP.getId().toString());
			json.put("loginName",oOP.getLoginName());
			json.put("hasChild",oOP.getHasChild());
			json.put("introduction",oOP.getIntroduction());
		}
		
		//OperatorDetail nOP = os.getOperatorDetail(nUid);
		List groupList = os.getGroupListByUser(nUid);
		this.groupFilter(json,groupList,oGroupList);
		response.getWriter().write(json.toString());
		return null;
		
		
		
	}
	private void groupFilter(JSONObject json, List groupList, List oGroupList){
		JSONArray fromArray = new JSONArray();
		JSONArray toArray = new JSONArray();
		
		if (oGroupList == null){
			for (Iterator it = groupList.iterator();it.hasNext();){
				JSONObject tmpJSON = new JSONObject();
				AuGroup group = (AuGroup)it.next();
				tmpJSON.put("gid",group.getId().toString());
				tmpJSON.put("groupName",group.getGroupName());
				fromArray.put(tmpJSON);
			}
		}else{
			for (Iterator it = groupList.iterator();it.hasNext();){
				boolean is_to_from = true;
				JSONObject tmpJSON = new JSONObject();
				AuGroup group = (AuGroup)it.next();
				tmpJSON.put("gid",group.getId().toString());
				tmpJSON.put("groupName",group.getGroupName());
				for (Iterator oIt = oGroupList.iterator();oIt.hasNext();){
					AuGroup oGroup = (AuGroup)oIt.next();
					if (group.getId().equals(oGroup.getId())){
						toArray.put(tmpJSON);
						is_to_from = false;
						break;
					}
				}
				if (is_to_from){
					fromArray.put(tmpJSON);
				}
			}
		}
		
		json.put("fromArray",fromArray);
		json.put("toArray",toArray);
	}
}
